
using System;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;

using System.IO;

using CStock;


using CUtil;




namespace CStock
{
	
	/// <summary>
	/// Summary description for Assets.
	/// </summary>
    public class NewsCollection : StockObjectCollection, INewsCollection
	{
        public NewsCollection()
        {
            this.iD = "TradeEconomyNews";
        }
        public new INews this[string name]
        {
            get
            {
                for (int i = 0; i < this.items.Count; i++)
                {
                    StockObjectBase myPf = this[i] as StockObjectBase;
                    if (myPf.ID == name)
                        return myPf as INews;
                }
                return null;
            }
        }

        public new INews this[int index]
        {
            get
            {
                try
                {
                    if ((index >= 0) && (index < this.items.Count))
                    {
                        object o = base[index];
                        return o as INews;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch
                {
                    Debug.Assert(false, "News : Error getting News");
                    return null;
                }
            }
            set
            {
                if ((index >= 0) && (index < this.items.Count))
                {
                    base[index] = value;
                }
            }
        }
        /// <summary>
        /// name convention for Assets: Contain the Trader name!!! Economy_Asset1
        /// </summary>
        /// <returns></returns>
        public override bool Serialize()
        {
            this.ResetPathFromConfiguration();

            for(int i = 0; i < this.items.Count; i++)
            {
                StockObjectBase myPf = this[i] as StockObjectBase;
                myPf.ResetPathFromConfiguration();
                myPf.Serialize();
            }
            return true;

        }
        /// <summary>
        /// name convention for Assets: Contain the Trader name!!! Economy_Asset1
        /// </summary>
        /// <returns></returns>
        public override object DeSerialize()
        {
            try
            {
                if (this.items.Count > 0)
                    this.items.RemoveRange(0, this.items.Count - 1);


                //Check ID!
                Debug.Assert(this.iD != null);
                Debug.Assert(this.iD != string.Empty);

                ProgramContext dh = ProgramContext.Instance;
                string path = dh.Configuration.Directories.NewsPath;

                //search for all Assets:

                //-------------------------------------------
                //search for files in that dir
                System.IO.DirectoryInfo di = new DirectoryInfo(path);
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);

                dh.Logfile.WriteTimeSpanLog("   Reading news Directory: " + path);

                foreach (FileInfo file in di.GetFiles("*.xml"))
                {

                    string strFile = file.Name;
                    string idAsset = CUtil.IOUtil.CutRightFourChars(strFile);
                    string id = idAsset;



                    if (id != string.Empty)
                    {
                        StockObjectBase portf = new News();
                        portf.ID = idAsset;

                        object o = new News();

                        if (portf.DeSerialize(ref o))
                        {
                            portf = o as News;
                            this.Add(portf);
                            dh.Logfile.WriteTimeSpanLog("       -success deserializing News: " + idAsset);
                        }
                    }

                }


            }
            catch (Exception ex)
            {
                Debug.Assert(false, "Error derializing News: " + ex.Message);
            }
            return new object();

        }
        public int Add(object obj)
        {
            News newAsset = (News)obj;

            if (this.Items.Count <= 0)
            {
                //if it is empty - add the Asset
                this.items.Add(newAsset);
                return 0;
            }
            else
            {
                //check if the Asset is already there!
                if (!this.items.Contains(newAsset))
                {
                    //No

                    //now add at a place so that we keep some order...
                    int index = this.Items.Count;
                    //for (int i = 0; i < this.Items.Count; i++)
                    //{
                    //    News listAsset = (News)this[i];

                    //}
                    this.Items.Insert(index, newAsset);
                    //OnChanged(EventArgs.Empty);
                    return index;
                }
                else
                {
                    //the Asset is already in the list!
                    Debug.Assert(false, "CAssets", "Error adding News - alread in the list " + newAsset.Name);
                    return -1;
                }
            }
        }
     
  
        /// <summary>
        /// ToAssetItemArray
        /// </summary>
        /// <returns></returns>
        public Asset[] ToAssetItemArray()
		{
			try
			{
                return (Asset[])this.items.ToArray(typeof(News));
			}
			catch (Exception exc)
			{
				Debug.WriteLine("DataItems - Error creating NewsItemArray" + exc.Message);
			}
			return null;
		}


        #region INewsCollection Members

        INews INewsCollection.this[int index]
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        INews INewsCollection.this[string myID]
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        #region IStockObjectCollection Members

        ArrayList IStockObjectCollection.Items
        {
            get
            {
                return null;
                //throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                //throw new Exception("The method or operation is not implemented.");
            }
        }

        object IStockObjectCollection.this[int index]
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        object IStockObjectCollection.this[string strID]
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        void IStockObjectCollection.Sort()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

    
    }
}
